package com.quectel.core.module.group.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.quectel.constant.global.SqlConstants;
import com.quectel.core.module.group.dao.SysGroupRegionDao;
import com.quectel.core.module.group.dto.SysGroupRegionDto;
import com.quectel.core.module.group.entity.SysGroupRegionEntity;
import com.quectel.core.module.group.service.SysGroupRegionService;
import com.quectel.util.common.CopyUtils;
import com.quectel.util.kit.Snowflake;
import org.apache.dubbo.config.annotation.DubboService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @author caolu
 * @email louis.cao@quectel.com
 * @date 2020-07-23 18:10:09
 */
@DubboService
public class SysGroupRegionServiceImpl implements SysGroupRegionService {

    private static final Logger LOGGER = LoggerFactory.getLogger(SysGroupRegionServiceImpl.class);

    @Override
    public SysGroupRegionDto queryOneByTenantAndRQ(Long tenantId, String code) {
        SysGroupRegionEntity sysGroupRegionEntitie = sysGroupRegionDao.selectOne(new LambdaQueryWrapper<SysGroupRegionEntity>()
                .eq(SysGroupRegionEntity::getTenantId, tenantId)
                .eq(SysGroupRegionEntity::getCode, code)
                .last(SqlConstants.MYSQL_LIMIT_ONE));
        return CopyUtils.copyObj(sysGroupRegionEntitie, SysGroupRegionDto.class);
    }

    @Override
    public void deleteByGroupIdAndLikeCode(Long id, String code) {
        sysGroupRegionDao.delete(new LambdaQueryWrapper<SysGroupRegionEntity>()
                .eq(SysGroupRegionEntity::getSysGroupId, id)
                .likeRight(SysGroupRegionEntity::getCode, code));
    }

    @Override
    public SysGroupRegionDto queryOneByGroupId(Long groupId) {
        SysGroupRegionEntity sysGroupRegionEntitie = sysGroupRegionDao.selectOne(new LambdaQueryWrapper<SysGroupRegionEntity>()
                .eq(SysGroupRegionEntity::getSysGroupId, groupId)
                .last(SqlConstants.MYSQL_LIMIT_ONE));
        return CopyUtils.copyObj(sysGroupRegionEntitie, SysGroupRegionDto.class);
    }

    @Autowired
    private SysGroupRegionDao sysGroupRegionDao;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteByGroupId(Long groupId) {
        sysGroupRegionDao.delete(
                new LambdaQueryWrapper<SysGroupRegionEntity>()
                        .eq(SysGroupRegionEntity::getSysGroupId, groupId)
        );
    }

    @Override
    public List<Long> queryVillageIds(Long groupId) {
        Set<Long> sets = sysGroupRegionDao.queryVillageIds(groupId);
        return new ArrayList<>(sets);
    }

    @Override
    public List<SysGroupRegionDto> queryList(Map<String, Object> params) {
        List<SysGroupRegionEntity> list = sysGroupRegionDao.queryList(params);
        return CopyUtils.copyList(list, SysGroupRegionDto.class);
    }

    @Override
    public int queryTotal(Map<String, Object> params) {
        return sysGroupRegionDao.queryTotal(params);
    }

    @Override
    public Long save(SysGroupRegionDto dto) {
        SysGroupRegionEntity entity = CopyUtils.copyObj(dto, SysGroupRegionEntity.class);
        entity.setId(Snowflake.nextId());
        sysGroupRegionDao.insert(entity);
        return entity.getId();
    }

    @Override
    public void updateById(SysGroupRegionDto dto) {
        SysGroupRegionEntity entity = CopyUtils.copyObj(dto, SysGroupRegionEntity.class);
        sysGroupRegionDao.updateById(entity);
    }

    @Override
    public SysGroupRegionDto selectById(Long id) {
        SysGroupRegionEntity entity = sysGroupRegionDao.selectById(id);
        return CopyUtils.copyObj(entity, SysGroupRegionDto.class);
    }

    @Override
    public void deleteById(Long id) {
        sysGroupRegionDao.deleteById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteBatch(Long[] ids) {
        for (Long id : ids) {
            deleteById(id);
        }
    }
}
